package prefuse.tp.svn;

import java.util.Iterator;
import java.util.Vector;

import prefuse.data.Graph;
import prefuse.data.Node;

public class GraphRadial_Affectations {
	public static final int MASTER = 0;
	public static final int MENTION = 1;
	public static final int SPECIALITE = 2;
	public static final int UE = 3;
	public static final int ETUDIANT = 4;
	
	@SuppressWarnings("unchecked")
	public void affecterAleatoirement(Graph g) {
		/*
		 * for (int i = 0; i < g.getNodeCount(); i++) { Node n = g.getNode(i);
		 * System.out.println(n); }
		 */
		Vector<Node> etudiants = new Vector<Node>();
		g.addColumn("type", int.class);
		for (int i = 0; i < g.getNodeCount(); i++) {
			Node n = g.getNode(i);
			if (n.get("Prenom") != null){
				etudiants.add(n);
				n.set("type", ETUDIANT);
				}
		}
		int NombreEtudiantAleatoire = 0;
		for(int i = 0; i < NombreEtudiantAleatoire; i++){
			Node n = g.addNode();
			n.set("Prenom", new String("Etudiant virtuel "+ i));
			n.set("Code", "0");
			n.set("type", ETUDIANT);
			etudiants.add(n);
		}
		
		
		Vector<Node> ues_s = new Vector<Node>();
		Vector<Node> ues_m = new Vector<Node>();
		for (int i = 0; i < g.getNodeCount(); i++) {
			Node n = g.getNode(i);
			if (n.get("Prenom") == null) {
				String code = (String) (n.get("Code"));
				String mention = (String) (n.get("Mention"));
				if (code != null && mention != null){
					if (!(code.equals("UFPRI") || code.equals("UE_UMINM") || code
							.equals("UE_UMINS"))) {
						if (mention.equals("IS"))
							ues_s.add(n);
						else
							ues_m.add(n);
						n.set("type", UE);
					}
				}
			}
		}
		
		for (int i = g.getEdgeCount()-1; i >= 0; i--)
			g.removeEdge(i);
		
		Node master = g.getNode(0);
		Node mentionIM = g.getNode(1);
		Node mentionIS = g.getNode(2);
		g.addEdge(master, mentionIM);
		g.addEdge(master, mentionIS);
		
		master.set("type", MASTER);
		mentionIM.set("type", MENTION);
		mentionIS.set("type", MENTION);
		
		Node[] specialites_m = new Node[3];
		for (int i = 0; i < 3; i++) {
			specialites_m[i] = g.addNode();
			g.addEdge(mentionIM, specialites_m[i]);
			specialites_m[i].set("type", SPECIALITE);
		}
		Node[] specialites_s = new Node[4];
		for (int i = 0; i < 4; i++) {
			specialites_s[i] = g.addNode();
			g.addEdge(mentionIS, specialites_s[i]);
			specialites_s[i].set("type", SPECIALITE);
		}
		specialites_s[0].set("Code", "CASAR");
		//specialites_s[0].set("Mention", "CASAR");
		specialites_s[1].set("Code", "I2A");
		//specialites_s[1].set("Mention", "I2A");
		specialites_s[2].set("Code", "DIWEB");
		//specialites_s[2].set("Mention", "DIWEB");
		specialites_s[3].set("Code", "GL");
		//specialites_s[3].set("Mention", "GL");

		specialites_m[0].set("Code", "BI");
		//specialites_m[0].set("Mention", "BI");
		specialites_m[1].set("Code", "IG3T");
		//specialites_m[1].set("Mention", "IG3T");
		specialites_m[2].set("Code", "CCI");
		//specialites_m[2].set("Mention", "CCI");

		// affectation des UEs aux spécialités :
		// UE_UMINM :
		for (Iterator it = ues_m.iterator(); it.hasNext();) {
					Node ue = (Node) it.next();
					int speElue = (int) (specialites_m.length * Math.random());
					g.addEdge(specialites_m[speElue], ue);
		}
		// UE_UMINS :
		for (Iterator it = ues_s.iterator(); it.hasNext();) {
			Node ue = (Node) it.next();
			int speElue = (int) (specialites_s.length * Math.random());
			g.addEdge(specialites_s[speElue], ue);
		}
		/*
		// affectation des étudiants :
		for (Iterator it = etudiants.iterator(); it.hasNext();) {
			Node etudiant = (Node) it.next();
			int mentionElue = (int) (2 * Math.random());
			Node[] specialites;
			if(mentionElue == 0)
				specialites = specialites_s;
			else
				specialites = specialites_m;
			Node speElue = specialites[(int) (specialites.length * Math.random())];
				
			Vector<Node> copieUE = new Vector<Node>();
			for (int j = 0; j < speElue.getChildCount(); j++) {
				copieUE.add(speElue.getChild(j));
			}
			
			for(int j = 6;j > 0 && copieUE.size()>0; j--) {
				int UeElue = (int)(copieUE.size()*Math.random());
				g.addEdge(copieUE.get(UeElue), etudiant);
				copieUE.remove(UeElue);
			}
		}*/
		
		// affectation des étudiants :
		for (Iterator it = etudiants.iterator(); it.hasNext();) {
			Node etudiant = (Node) it.next();
			etudiant.set("type", ETUDIANT);
			int mentionElue = (int) (2 * Math.random());
			Vector<Node> ues;
			if(mentionElue == 0)
				ues = ues_m;
			else
				ues = ues_s;
				
			Vector<Node> copieUE = new Vector<Node>();
			for (Iterator it2 = ues.iterator(); it2.hasNext();) {
				Node ue = (Node) it2.next();
				copieUE.add(ue);
			}
			
			for(int j = 6;j > 0 && copieUE.size()>0; j--) {
				int UeElue = (int)(copieUE.size()*Math.random());
				g.addEdge(copieUE.get(UeElue), etudiant);
				copieUE.remove(UeElue);
			}
		}

	}
}
